﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XStudio.XCRF.Web.Exceptions;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Data.Enums;

namespace XStudio.XCRF.Web.Common.Sessions
{
    /// <summary>
    /// 与登陆状态相关的Session信息
    /// </summary>
    public class LoginSession : BaseSession
    {
        #region 键名
        /// <summary>
        /// 登陆名键名
        /// </summary>
        private const string KEY_LoginName = "LOGIN_NAME";
        /// <summary>
        /// 登陆ID键名
        /// </summary>
        private const string KEY_LoginID = "LOGIN_ID";
        /// <summary>
        /// 用户类型键名
        /// </summary>
        private const string KEY_MemberType = "MEMBER_TYPE";
        /// <summary>
        /// 医院ID键名
        /// </summary>
        private const string KEY_HospitalID = "HOSPITAL_ID";
        /// <summary>
        /// 标识成员信息是否是第一次修改的键名
        /// </summary>
        private const string KEY_IsMemberFirst = "IS_MEMBER_FIRST";
        /// <summary>
        /// 标识医院信息是否是第一次修改的键名
        /// </summary>
        private const string KEY_IsHospitalFirst = "IS_HOSPITAL_FIRST";
        #endregion 键名

        #region 属性
        /// <summary>
        /// 获取和设置Session中的登陆名
        /// </summary>
        public static string LoginName
        {
            get
            {
                object obj = CurrentSession[KEY_LoginName];
                if (obj == null)
                    return null;
                return (string)obj;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    CurrentSession[KEY_LoginName] = null;
                else CurrentSession[KEY_LoginName] = value;
            }
        }

        /// <summary>
        /// 获取和设置Session中的登陆ID
        /// </summary>
        public static Guid? LoginID
        {
            get
            {
                object obj = CurrentSession[KEY_LoginID];
                if (obj == null)
                    return null;
                return (Guid)obj;
            }
            set
            {
                if (value == null || value == Guid.Empty)
                    CurrentSession[KEY_LoginID] = null;
                else CurrentSession[KEY_LoginID] = value;
            }
        }

        /// <summary>
        /// 获取和设置Session中的成员类型
        /// </summary>
        public static MemberTypes? MemberType
        {
            get
            {
                object obj = CurrentSession[KEY_MemberType];
                if (obj == null)
                    return null;
                return (MemberTypes)obj;
            }
            set
            {
                if (value == null)
                    CurrentSession[KEY_MemberType] = null;
                else CurrentSession[KEY_MemberType] = value;
            }
        }

        /// <summary>
        /// 获取当前用户是否是管理员
        /// </summary>
        public static bool IsAdministrator
        {
            get
            {
                return MemberType == MemberTypes.Administrator;
            }
        }
        /// <summary>
        /// 获取当前用户是否是主管
        /// </summary>
        public static bool IsOwner
        {
            get
            {
                return MemberType == MemberTypes.Owner;
            }
        }
        /// <summary>
        /// 获取当前用户是否是研究者
        /// </summary>
        public static bool IsResearcher
        {
            get
            {
                return MemberType == MemberTypes.Researcher;
            }
        }
        /// <summary>
        /// 获取和设置Session中的医院ID
        /// </summary>
        public static Guid? HospitalID
        {
            get
            {
                object obj = CurrentSession[KEY_HospitalID];
                if (obj == null)
                    return null;
                return (Guid)obj;
            }
            set
            {
                if (value == null)
                    CurrentSession[KEY_HospitalID] = null;
                else CurrentSession[KEY_HospitalID] = value;
            }
        }

        /// <summary>
        /// 获取和设置成员是否是第一次登陆
        /// </summary>
        public static bool IsMemberFirst
        {
            get
            {
                object obj = CurrentSession[KEY_IsMemberFirst];
                if (obj == null)
                    return false;
                return true;
            }
            set
            {
                if (value)
                    CurrentSession[KEY_IsMemberFirst] = true;
                else CurrentSession[KEY_IsMemberFirst] = null;
            }
        }

        /// <summary>
        /// 获取和设置医院是否是第一次登陆
        /// </summary>
        public static bool IsHospitalFirst
        {
            get
            {
                object obj = CurrentSession[KEY_IsHospitalFirst];
                if (obj == null)
                    return false;
                return true;
            }
            set
            {
                if (value)
                    CurrentSession[KEY_IsHospitalFirst] = true;
                else CurrentSession[KEY_IsHospitalFirst] = null;
            }
        }

        /// <summary>
        /// 获取是否已登陆
        /// </summary>
        public static bool IsLoggedIn
        {
            get
            {
                return CurrentSession[KEY_LoginID] != null;
            }
        }
        #endregion 属性

        #region 操作方法
        /// <summary>
        /// 在Session中记录登陆信息
        /// </summary>
        /// <param name="member">成员对象</param>
        public static void LogIn(Member member)
        {
            if (LoginID != null)
                throw new AlreadyLoggedInException();
            LoginID = member.ID;
            LoginName = member.LoginName;
            MemberType = (MemberTypes)member.Type;
            HospitalID = member.HospitalID;
            IsMemberFirst = member.IsFirst;
            IsHospitalFirst = member.Hospital.IsFirst;
        }

        /// <summary>
        /// 在Session中注销登录
        /// </summary>
        public static void LogOut()
        {
            LoginID = null;
            LoginName = null;
            MemberType = null;
            HospitalID = null;
            IsMemberFirst = false;
            IsHospitalFirst = false;
        }
        #endregion 操作方法
    }
}